# -*- coding: utf-8 -*-
"""
Created on Mon Nov 29 2021

@author: ShiFeng
"""
import numpy as np
import pandas as pd
import json
import os
current_path = os.path.dirname(os.path.abspath(__file__))
os.chdir(current_path)
# define series length 
SERIES_LENGTH = 96
# dataset filenames
dataset_filename1 = "2U3mSACmax.csv"
dataset_filename2 = "6U3mSACmax.csv"

# label filename
labels_name = "labels.json"
# output filename
output_dataset_filename = dataset_filename1[0:2]+dataset_filename2[:-4]+'_length_96'+'.csv'

print(output_dataset_filename)

print("loading data")
# each file has 591 rows
x = pd.read_csv(dataset_filename1, header=None, usecols=[0,1,2,3,4,5,6])
y = pd.read_csv(dataset_filename2, header=None, usecols=[0,1,2,3,4,5,6])
# 
# print(x)
# print(y)
# print(x.shape[0])

print("loading labels.json")
# Load labels, file can be found in challenge description
with open(labels_name, "r") as stream_json:
  labels = json.load(stream_json)
print(labels)
# print(int(dataset_filename1[0]))
# calculate dipole electroic length
dipole_length1 = np.zeros((x.shape[0], 1)) + int(dataset_filename1[0]) *44.45/1000 
dipole_length2 = np.zeros((x.shape[0], 1)) + int(dataset_filename2[0]) *44.45/1000 

# print(type(x))
# print(x[0].astype(float).values.reshape(x.shape[0], 1))
x_series_data0 = dipole_length1 *x[0].values.reshape(x.shape[0], 1)/0.3
print(x_series_data0.shape)
# print(series_data0)
print("---------------------------")
x_series_data1 = x[1].values.reshape(x.shape[0], 1)
x_series_data2 = x[2].values.reshape(x.shape[0], 1)
x_series_data3 = x[3].values.reshape(x.shape[0], 1)
x_series_data4 = x[4].values.reshape(x.shape[0], 1)
x_series_data5 = x[5].values.reshape(x.shape[0], 1)
# x_series_data6 = x[6].values.reshape(x.shape[0], 1)
# another data file
y_series_data0 = dipole_length1 *y[0].values.reshape(y.shape[0], 1)/0.3
y_series_data1 = y[1].values.reshape(y.shape[0], 1)
y_series_data2 = y[2].values.reshape(y.shape[0], 1)
y_series_data3 = y[3].values.reshape(y.shape[0], 1)
y_series_data4 = y[4].values.reshape(y.shape[0], 1)
y_series_data5 = y[5].values.reshape(y.shape[0], 1)
y_series_data6 = y[6].values.reshape(y.shape[0], 1)
# vstack the two data files
x_series_data0 = np.vstack((x_series_data0,  y_series_data0))
x_series_data1 = np.vstack((x_series_data1,  y_series_data1))
x_series_data2 = np.vstack((x_series_data2,  y_series_data2))
x_series_data3 = np.vstack((x_series_data3,  y_series_data3))
x_series_data4 = np.vstack((x_series_data4,  y_series_data4))
x_series_data5 = np.vstack((x_series_data5,  y_series_data5))
# x_series_data6 = np.vstack((x_series_data6,  y_series_data6))

print(x_series_data0.shape)

# 
x_middle_data0 = []
for i in range(x.shape[0] + y.shape[0] - SERIES_LENGTH + 1):
  x_middle_data0.append(x_series_data0[i :i+SERIES_LENGTH].reshape(1,SERIES_LENGTH))
x_middle_data0 = np.array(x_middle_data0).transpose(1,0,2).reshape(x.shape[0]+ y.shape[0]-SERIES_LENGTH+1, SERIES_LENGTH)

x_middle_data1 = []
for i in range(x.shape[0] + y.shape[0] - SERIES_LENGTH + 1):
  x_middle_data1.append(x_series_data1[i :i+SERIES_LENGTH].reshape(1,SERIES_LENGTH))
x_middle_data1 = np.array(x_middle_data1).transpose(1,0,2).reshape(x.shape[0]+ y.shape[0]-SERIES_LENGTH+1, SERIES_LENGTH)

x_middle_data2 = []
for i in range(x.shape[0] + y.shape[0] - SERIES_LENGTH + 1):
  x_middle_data2.append(x_series_data2[i :i+SERIES_LENGTH].reshape(1,SERIES_LENGTH))
x_middle_data2 = np.array(x_middle_data2).transpose(1,0,2).reshape(x.shape[0]+ y.shape[0]-SERIES_LENGTH+1, SERIES_LENGTH)

x_middle_data3 = []
for i in range(x.shape[0] + y.shape[0] - SERIES_LENGTH + 1):
  x_middle_data3.append(x_series_data3[i :i+SERIES_LENGTH].reshape(1,SERIES_LENGTH))
x_middle_data3 = np.array(x_middle_data3).transpose(1,0,2).reshape(x.shape[0]+ y.shape[0]-SERIES_LENGTH+1, SERIES_LENGTH)

x_middle_data4 = []
for i in range(x.shape[0] + y.shape[0] - SERIES_LENGTH + 1):
  x_middle_data4.append(x_series_data4[i :i+SERIES_LENGTH].reshape(1,SERIES_LENGTH))
x_middle_data4 = np.array(x_middle_data4).transpose(1,0,2).reshape(x.shape[0]+ y.shape[0]-SERIES_LENGTH+1, SERIES_LENGTH)

x_middle_data5 = []
for i in range(x.shape[0] + y.shape[0] - SERIES_LENGTH + 1):
  x_middle_data5.append(x_series_data5[i :i+SERIES_LENGTH].reshape(1,SERIES_LENGTH))
x_middle_data5 = np.array(x_middle_data5).transpose(1,0,2).reshape(x.shape[0]+ y.shape[0]-SERIES_LENGTH+1, SERIES_LENGTH)
print(x_middle_data5.shape)

# print(type(x_middle_data5))
# print(np.array(x_middle_data5).shape)
# print(np.array(x_middle_data5))
# print(type(x_middle_data5))
# print(x_middle_data5)

x_middle_data = np.hstack((x_middle_data0, x_middle_data1, x_middle_data2, x_middle_data3, x_middle_data4))
print(x_middle_data.shape)
col_title1 = []
col_title1 = [f"Elength_"+f"{i}" for i in range(SERIES_LENGTH)]
col_title2 = []
col_title2 = [f"MeamtHigh_"+f"{i}" for i in range(SERIES_LENGTH)]
col_title3 = []
col_title3 = [f"SphereEt_"+f"{i}" for i in range(SERIES_LENGTH)]
col_title4 = []
col_title4 = [f"ScanHigh_"+f"{i}" for i in range(SERIES_LENGTH)]
col_title5 = []
col_title5 = [f"ScanEtz_"+f"{i}" for i in range(SERIES_LENGTH)]

col_mix = col_title1 + col_title2 + col_title3 + col_title4 + col_title5
# print(col_mix)
x_middle_data = pd.DataFrame(x_middle_data, columns=col_mix)
# generate the final dataset 
x_middle_data.to_csv(output_dataset_filename)



